<?php

class ConsumForm extends BaseConsumForm
{
    const APARTAMENT_ID = 'apartament_id';
    const CRITERIU_ID   = 'criteriu_id';
    const VALOARE       = 'valoare';
    const LUNA          = 'luna';

    public function configure()
    {

    }

    protected function configureWidgets()
    {
        $this->setWidget(self::APARTAMENT_ID, new sfWidgetFormInputText());
        $this->setWidget(self::CRITERIU_ID, new sfWidgetFormInputText());
    }

    protected function configureValidators()
    {
        // reset validators
        $this->setValidator(self::APARTAMENT_ID, new sfValidatorPass());
        $this->setValidator(self::CRITERIU_ID, new sfValidatorPass());
        $this->setValidator(self::VALOARE, new sfValidatorPass());

    }

    public function saveObject($values)
    {
        // only valid values are saved
        if ( ApartamentPeer::validate($values[self::APARTAMENT_ID])
             && CriteriuPeer::validateConsum($values[self::CRITERIU_ID])
             && ConsumPeer::validate($values[self::VALOARE]) )
        {
            return parent::saveObject($values);
        }

        return null;
    }

    protected function _updateObject($values)
    {
        // daca LUNA nu este setata, se considera valoarea implicita
        if (!isset($values[self::LUNA]))
        {
            $values[self::LUNA] = App::getWorkingMonth();
        }

        // verific daca se editeaza sau se adauga un consum
        $criteria = new Criteria();
        $criteria
            ->add(ConsumPeer::LUNA, $values[self::LUNA])
            ->add(ConsumPeer::APARTAMENT_ID, $values[self::APARTAMENT_ID])
            ->add(ConsumPeer::CRITERIU_ID, $values[self::CRITERIU_ID]);

        $consumObject = ConsumPeer::doSelectOne($criteria);
        if ($consumObject)
        {
            $values['id'] = $consumObject->getId();
            $this->isNew = false;
            $this->object = $consumObject;
        }

        return $values;
    }

}
